<template>
  <div>
    <el-input style="width: 200px; margin: 0 10px" placeholder="请输入文件名" suffix-icon="el-icon-search"
              v-model="filename"/>
    <el-button type="primary" @click="search">搜索</el-button>
    <div>
      <el-button type="primary" style="margin: 10px" @click="dialogFormVisible = true">新增<i
          class="el-icon-circle-plus"></i></el-button>
      <el-button type="danger" style="margin: 10px" @click="handleDelBatch">批量删除<i
          class="el-icon-remove-outline"></i></el-button>
      <el-button type="primary" style="margin: 10px">导入<i class="el-icon-upload2"></i></el-button>
    </div>

    <el-dialog title="收货地址" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="城市名" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="人口" :label-width="formLabelWidth">
          <el-input v-model="form.population" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="活动区域" :label-width="formLabelWidth">
          <el-select v-model="form.region" placeholder="请选择活动区域">
            <el-option label="区域一" value="shanghai"></el-option>
            <el-option label="区域二" value="beijing"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSave">确 定</el-button>
      </div>
    </el-dialog>

    <el-table :data="tableData" style="margin: 10px; width: 1600px" border stripe>
      <el-table-column prop="id" label="ID" width="140">
      </el-table-column>
<!--      <el-table-column prop="saveName" label="文件名" style="display: none">-->
<!--      </el-table-column>-->
      <el-table-column prop="originName" label="文件名" width="300">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" @click="download(scope.row.id)">下载<i class="el-icon-edit"></i></el-button>


        </template>
      </el-table-column>
    </el-table>
    <div class="block" style="padding: 5px">

      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
  </div>
</template>
<script>
import service from "@/utils/request";
import ElementUI from "element-ui";

export default {
  created() {
    this.load();


  },
  data() {

    return {
      // tableData: Array(15).fill(item),
      tableData: [],
      pageSize: 10,
      total: 100,
      currentPage: 1,
      filename: '',


      dialogFormVisible: false,
      form: {
        name: '',
        population: '',

      },
      formLabelWidth: '120px'
    }
  },
  methods: {

    load() {
      service.get('/file/page', {
        params: {
          pageSize: this.pageSize,
          pageNum: this.currentPage,
        }
      }).then(response => {
            if (response.code == 20000) {
              this.tableData = response.data.records;
              this.total = response.data.total;
              this.currentPage = response.data.current;
              this.pageSize = response.data.size;
            }
          },
          error => {
            ElementUI.Message({
              message: error.message,
              type: 'error'
            })
          }
      );

    },
    downloadFile(res) {
      if (!res) {
        return;
      }
      console.log(res)
      //                            Content-Disposition
      const fileName = res.headers["content-disposition"].split("=")[1];
      const blob = new Blob([res.data], {
        type: 'application/zip'
      });
      const url = window.URL.createObjectURL(blob);
      const aLink = document.createElement("a");
      aLink.style.display = "none";
      aLink.href = url;
      aLink.setAttribute("download", decodeURI(fileName));
      document.body.appendChild(aLink);
      aLink.click();
      document.body.removeChild(aLink);
      window.URL.revokeObjectURL(url);
    },
    download(id) {
      let url = "http://localhost:9090/file/" + id;
      service({
        url: url,
        method: "get",
        responseType: 'blob',
      }).then(res => {

        this.downloadFile(res);
      })
      // window.open('http://localhost:9090/file/' + id)
      // service.get('/file/' + id, {
      //
      // }).then(response => {
      //       if (response.code == 20000) {
      //
      //       }
      //     },
      //     error => {
      //       ElementUI.Message({
      //         message: error.message,
      //         type: 'error'
      //       })
      //     }
      // );

    },
    handleSizeChange(val) {
      this.pageSize = val
      this.load()
    }
    ,
    handleCurrentChange(val) {
      this.currentPage = val
      this.load()
    }
    ,

    handleDel(id) {
      service.delete('/file?id=' + id).then(response => {
        this.$message.success('删除成功!');
        this.load()
      })
    }
    ,
    search() {
      if (!this.population.isInteger) {
        return;
      }
      this.currentPage = 1
      this.load()
    }
    ,
    handleDelBatch() {
      console.log(this.$route)
      // console.log(this.$router)
    }
    ,
    validatePopulation() {
      if (!this.population.isInteger) {
        this.$message.info('dfd')
        this.population = ''
      }

    }
    ,
    handleSave(){

    },
    exp() {
      window.open('http://localhost:9090/city/export')
      // service.get('/export').then(response => {})
      //
      //


    }

  }
}
</script>

<style scoped>

</style>